import {reactive} from "plain-design-composition";

export function createPageLoading(min = 100) {
    let just = 0
    const proxy = reactive({
        loading: false,
        loadingMessage: '',
        start: (loadingMessage?: string) => {
            proxy.loading = true
            proxy.loadingMessage = loadingMessage || '加载中'
            just = Date.now()
        },
        done: () => {
            const now = Date.now()
            const dur = now - just
            if (dur > min) {
                proxy.loading = false
            } else {
                setTimeout(() => proxy.loading = false, 300 - dur)
            }
        },
    })
    return proxy
}
